package edu.pku.sei.transformation.editor.views.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

public class ContentDialog {
	private int returnCode = SWT.CANCEL;
	private String documentResult = "";
	public Text docText;
	protected Shell shell;
	private String title = "content";

	public ContentDialog() {
		shell = new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getWorkbenchWindow().getShell(),
				SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
	}

	public int open() {
		final Display display = Display.getDefault();
		createDialogArea(shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		return returnCode;
	}

	protected Control createDialogArea(Composite parent) {
		shell.setText(title);
		shell.setSize(300, 500);

		GridLayout shellGL = new GridLayout();
		shellGL.numColumns = 1;
		shell.setLayout(shellGL);

		docText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
				| SWT.H_SCROLL);
		docText.setText(documentResult);

		GridData d = new GridData();
		d.grabExcessHorizontalSpace = true;
		d.grabExcessVerticalSpace = true;
		d.horizontalAlignment = SWT.FILL;
		d.verticalAlignment = SWT.FILL;
		docText.setLayoutData(d);

		Composite buttonComp = new Composite(shell, SWT.NONE);
		GridData buttonCompGD = new GridData();
		buttonCompGD.horizontalAlignment = GridData.CENTER;
		buttonComp.setLayoutData(buttonCompGD);

		RowLayout buttonCompRL = new RowLayout();
		buttonCompRL.pack = false;
		buttonCompRL.spacing = 5;
		buttonComp.setLayout(buttonCompRL);
		// OK button
		Button okButton = new Button(buttonComp, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				documentResult = docText.getText();
				returnCode = SWT.OK;
				shell.dispose();
			}
		});
		okButton.setText("OK");

		Button cancelButton = new Button(buttonComp, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				returnCode = SWT.CANCEL;
				shell.dispose();
			}
		});
		cancelButton.setText("Cancel");

		return shell;
	}

	public String getDocumentResult() {
		return documentResult;
	}

	public void setDocumentResult(String documentResult) {
		this.documentResult = documentResult;
	}
	
	public void setTitle(String s) {
		this.title = s;
	}
}
